﻿Imports TBCC.UtilityMobile


Public Class StartupDetailViewForm(Of T1 As {StartUpDetailTypeItem, New}, T2 As {StartUpTypeBase, New})

    Private _pi As ProjectItem
    Protected _sti As T2
    Protected _il As IList(Of T1) = New List(Of T1)
    Private _sdts As StartUpDetailTypeService
    Private _lastTime As DateTime
    Private _isModify As Boolean

#Region " 构造函数 "

    Public Sub New(ByVal pi As ProjectItem, ByVal sti As T2)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _pi = pi
        _sti = sti
        If pi IsNot Nothing AndAlso sti IsNot Nothing Then
            _sdts = New StartUpDetailTypeService(pi, _sti)
        End If
        Me.dgvStartup.AutoGenerateColumns = False
        'Me.dgvStartup.DataSource = _il
        'Me.dgvStartup.da = _il

        If pi IsNot Nothing Then
            Me.Text &= String.Format(" [终端:{0}]", pi.ProjectName)
        Else
            Me.Text &= " [终端:未知]"
        End If
        If _sti IsNot Nothing Then
            Me.Text &= String.Format(" ({0} ～ {1})", _
                                IIf(sti.BeginTime = Nothing, String.Empty, sti.BeginTime.ToString), _
                                IIf(sti.EndTime = Nothing, String.Empty, sti.EndTime.ToString))
        End If
    End Sub

#End Region

    Private Sub StartupDetailAddForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToParent()

        LoadData()
    End Sub

    Private Sub StartupDetailViewForm_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If Asc(e.KeyChar) = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub StartupDetailViewForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If _isModify = True Then
            Select Case msg.ShowYesNoCancel("详细启停记录已经修改，是否需要保存？")
                Case Windows.Forms.DialogResult.Yes
                    tsbSave_Click(sender, e)

                Case Windows.Forms.DialogResult.Cancel
                    e.Cancel = True

            End Select
        End If
    End Sub

    Private Sub LoadData()

        _LoadData()

        Me.bsStartupDetail.DataSource = _il

        If _il IsNot Nothing AndAlso _il.Count > 0 Then
            _lastTime = _il.Item(_il.Count - 1).EndTime
        Else
            _lastTime = _sti.BeginTime
        End If

    End Sub

    Protected Overridable Sub _LoadData()

        If _sdts IsNot Nothing Then
            _il = _sdts.GetStartUpDetailGroupList(Of T1)(_sti.ID)
            'For i As Integer = 0 To 4
            '    Dim sdti As New StartUpDetailTypeItem()
            '    sdti.SeqNo = i + 1
            '    sdti.BeginTime = DateTime.Now.AddDays(-1)
            '    sdti.EndTime = DateTime.Now.AddHours(i)
            '    _il.Add(sdti)
            'Next

            Me.bsStartupDetail.DataSource = _il

            'Me.dgvStartup.DataSource = Nothing
            'Me.dgvStartup.DataSource = il
        Else
            If _sti IsNot Nothing Then
                _lastTime = _sti.BeginTime
            End If
        End If

    End Sub

#Region " DataGridView消息处理 "

    Private Sub dgvStartup_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgvStartup.DataError
        e.Cancel = True
    End Sub

    Private Sub dgvStartup_UserAddedRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles dgvStartup.UserAddedRow
        e.Row.Cells(0).Value = e.Row.Index + 1
    End Sub

#End Region

#Region " 工具栏操作 "

    Private Sub tsbInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbInsert.Click, tsmiInsert.Click

        Insert()

    End Sub

    Private Sub tsbDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbDelete.Click, tsmiDelete.Click

        Delete()

    End Sub

    Private Sub tsbEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEdit.Click, tsmiEdit.Click

        Edit()

    End Sub

    Private Sub tsbSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSave.Click

        If Save() = True Then
            _isModify = False
            msg.Show("保存成功。")
        Else
            msg.ShowError("保存失败。")
        End If

    End Sub

    Private Sub tsbHisData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbHisData.Click, tsmiHisData.Click, dgvStartup.DoubleClick

        HistoryData()

    End Sub

    Private Sub Insert()
        Dim sdti As New T1()
        sdti.BeginTime = _lastTime
        sdti.EndTime = _lastTime
        Dim frm As New StartupDetailItemForm(ActiveType.Create, sdti, _sti)
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            sdti.SeqNo = Me.bsStartupDetail.Count + 1
            sdti.ParentID = _sti.ID
            Dim index As Integer = -1
            For i As Integer = 0 To Me.bsStartupDetail.Count - 1
                If Me.bsStartupDetail.Item(i).BeginTime > sdti.BeginTime Then
                    index = i
                    Exit For
                End If
            Next
            If index <> -1 Then
                Me.bsStartupDetail.Insert(index, sdti)
                ReloadSeqNo()
            Else
                _lastTime = sdti.EndTime
                Me.bsStartupDetail.Add(sdti)
            End If
            _isModify = True
        End If
    End Sub

    Private Sub Delete()
        If Me.bsStartupDetail.Current IsNot Nothing Then
            If msg.ShowYesNo("是否确定要删除该记录？") = Windows.Forms.DialogResult.Yes Then
                If Me.bsStartupDetail.IndexOf(Me.bsStartupDetail.Current) = Me.bsStartupDetail.Count - 1 AndAlso Me.bsStartupDetail.Count > 1 Then
                    _lastTime = Me.bsStartupDetail.Item(Me.bsStartupDetail.Count - 2).EndTime
                End If
                Me.bsStartupDetail.RemoveCurrent()
                Me.ReloadSeqNo()
                _isModify = True
            End If
        End If
    End Sub

    Private Sub Edit()
        Dim sdti As T1
        sdti = Me.bsStartupDetail.Current

        If sdti IsNot Nothing Then
            Dim frm As New StartupDetailItemForm(ActiveType.Modify, sdti, _sti)
            If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
                If sdti.SeqNo = Me.bsStartupDetail.Count Then 'LastItem
                    _lastTime = frm.SDTI.EndTime
                End If
                _isModify = True
            End If
        End If
    End Sub

    Private Function Save() As Boolean

        Return _Save()

    End Function

    Protected Overridable Function _Save() As Boolean
        If _sdts IsNot Nothing Then
            Return _sdts.UpdateStartUpDetailGroupList(_il)
        End If

        Return False
    End Function

    Private Sub ReloadSeqNo()
        For i As Integer = 0 To Me.bsStartupDetail.Count - 1
            Me.bsStartupDetail.Item(i).SeqNo = i + 1
        Next
    End Sub

    Private Sub HistoryData()
        If Me.bsStartupDetail.Current IsNot Nothing Then
            Dim sdti As StartUpDetailTypeItem = Me.bsStartupDetail.Current
            Dim frm As New HDViewForm(_pi, sdti)
            frm.Show(Me)
        End If
    End Sub

#End Region

End Class